﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using DAL.EstRec;

namespace BAL.EstRec
{
    public class CapturaPivotFDV : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        #region properties
        
        private Pos _pos;
        private Recurso _recurso;
        protected int _cantidad;

        //private int _tiempoImplementacion;
        //private int _visitasSugeridas;
        //private string _iniciativas;

        public Pos pos
        {
            get { return _pos; }
            set {
                if (value != pos)
                {
                    this._pos = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Pos"));
                    }
                }
            }//set
        }

        public Recurso recurso
        {
            get { return _recurso; }
            set {
                if (value != recurso)
                {
                    this._recurso = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Recurso"));
                    }
                }
            }//set
        }

        public virtual int cantidad
        {
            get { return _cantidad; }
            set {
                if (value != cantidad)
                {
                    this._cantidad = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Cantidad"));
                    }
                }
            }
        }

        /*
        public int tiempoImplementacion
        {
            get { return _tiempoImplementacion; }
            set { this._tiempoImplementacion = value; }
        }

        public int visitasSugeridas
        {
            get { return _visitasSugeridas; }
            set { this._visitasSugeridas = value; }
        }

        public string iniciativas
        {
            get { return _iniciativas; }
            set { this._iniciativas = value; }
        }
        */
        #endregion
        #region METODOS
        public static List<CapturaPivotFDV> GetCapturaPivotFDV(long unidFdvBgtVersion,string posicionCode)
        {
            List<CapturaPivotFDV> capturaPivotFDV;

            DataTable dt;
            string sqlStmt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_GET_PIVOT_FDV "
                                + "@UNID_FDV_BGT_VERSION=@@par1"
                                + ",@POSITION_CODE='@@par2'";
            sqlStmt = sqlStmt.Replace("@@par1", unidFdvBgtVersion.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", posicionCode.ToString());
            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                //Invertido
                capturaPivotFDV = dt.AsEnumerable().Select(
                            row => new CapturaPivotFDV
                            {
                                cantidad = (row.IsNull("CANTIDAD") == true) ? 0 : (int)row["CANTIDAD"]
                                ,
                                pos = new Pos(Convert.ToString((row.IsNull("ID_POS") == true) ? "" : row["ID_POS"]),
                                              Convert.ToString((row.IsNull("POS") == true) ? "" : row["POS"]),
                                              Convert.ToString((row.IsNull("RAZON_SOCIAL") == true) ? "" : row["RAZON_SOCIAL"]),
                                              Convert.ToString((row.IsNull("CODIGO_POSTAL") == true) ? "" : row["CODIGO_POSTAL"]))
                                ,
                                recurso = new Recurso(Convert.ToString((row.IsNull("ID_RECURSO") == true) ? "" : row["ID_RECURSO"]),
                                                      Convert.ToString((row.IsNull("RECURSO") == true) ? "" : row["RECURSO"]),
                                                      Convert.ToString((row.IsNull("ID_INICIATIVA") == true) ? "" : row["ID_INICIATIVA"]),
                                                      Convert.ToString((row.IsNull("INICIATIVA") == true) ? "" : row["INICIATIVA"]),
                                                      Convert.ToString((row.IsNull("COSTO") == true) ? "" : row["COSTO"]),
                                                      Convert.ToInt16( (row.IsNull("TIEMPO_IMPLEMENTACION")==true)?0:row["TIEMPO_IMPLEMENTACION"] ),
                                                      Convert.ToInt16( (row.IsNull("VISITAS_SUGERIDAS") == true) ? 0 : row["VISITAS_SUGERIDAS"]),
                                                      Convert.ToString((row.IsNull("TIPO_RECURSO") == true) ? 0 : row["TIPO_RECURSO"]),
                                                      Convert.ToBoolean( (row.IsNull("EXCLUIR_REGLA_INICIATIVA") == true) ? 0 : row["EXCLUIR_REGLA_INICIATIVA"]),
                                                      Convert.ToBoolean((row.IsNull("EXCLUIR_REGLA_MAX_RECURSOS") == true) ? 0 : row["EXCLUIR_REGLA_MAX_RECURSOS"]))
                            }
                        ).ToList();
                return capturaPivotFDV;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;
        }
        #endregion
        public CapturaPivotFDV(){ }
        public CapturaPivotFDV(Pos pos)
        {
            this.pos = pos;
        }//

        public CapturaPivotFDV(Pos pos, Recurso recurso)
        {
            this.pos = pos;
            this.recurso = recurso;
        }//

        public CapturaPivotFDV(Pos pos, Recurso recurso, int cantidad)//int tiempoImp, int vistiasSug)
        {
            this.pos = pos;
            this.recurso = recurso;
            this.cantidad = cantidad;
            //this.tiempoImplementacion = tiempoImp;
            //this.visitasSugeridas = vistiasSug;
        }//


        #region DAL Methods
        public static void FlushCollection(CollectionsPivotFDV col,long unidVersion)
        {
            int flushEvery = 5000;
            int count = 0;
            string baseSqlStmt = "exec SP_UPSERT_FDVBGT @unidVersion="+unidVersion.ToString()
                                    + ",@idPos='@@par2'"
                                    + ",@idRecurso='@@par3'"
                                    + ",@cantidad=@@par4;";
            string sqlStmt = "";

            foreach ( VMCapturaPivotFDV cpf in col)
            {
                if (cpf.IsModified)
                {
                    sqlStmt += baseSqlStmt.Replace(
                                    "@@par2", cpf.pos.idPos.ToString()
                                ).Replace(
                                    "@@par3", cpf.recurso.idRecurso.ToString()
                                ).Replace(
                                    "@@par4", cpf.cantidad.ToString()
                                );
                    sqlStmt += "; ";

                    if (count == flushEvery)
                    {
                        try
                        {
                            SrvDB.ExecuteNonQuery(sqlStmt);
                            sqlStmt = "";
                            count = 0;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                    }

                    count++; 
                }
            }

            if (!String.IsNullOrEmpty(sqlStmt))
            {
                try
                {
                    SrvDB.ExecuteNonQuery(sqlStmt);
                    sqlStmt = "";
                    count = 0;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        #endregion


    }// end class
}
